//
//  SHWelcomeViewController.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/26.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

class SHWelcomeViewController: UIViewController {
    
    //MARK: - 懒加载控件
    // 背景图片
    private lazy var bgImageView: UIImageView = UIImageView(imgName: "ad_background")
    // 头像
    private lazy var avatarImageView: UIImageView = {
        let imgV = UIImageView()
        imgV.sh_setImage(SHUserAccountViewModel.sharedTools.userAccount?.avatar_large, placeholderImgName: "avatar_default_big")
        imgV.layer.borderColor = K_THEME_COLOR.CGColor
        imgV.layer.borderWidth = 2
        imgV.layer.cornerRadius = 45
        imgV.layer.masksToBounds = true
        
        return imgV
    }()
    // 欢迎回来
    private lazy var messageLabel: UILabel = {
        let lab = UILabel(
            text: "欢迎回来",
            textColor: UIColor.darkGrayColor(),
            font: 18,
            textAlignment: .Center)
        lab.alpha = 0
        return lab
    }()
    
    //MARK: - 设置视图
    override func loadView() {
        view = bgImageView
        
        setupUI()
    }

    private func setupUI() {
        view.addSubview(avatarImageView)
        view.addSubview(messageLabel)
        
        avatarImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(avatarImageView.snp_bottom).offset(16)
        }
    }
    
    override func viewDidAppear(animated: Bool) {
         super.viewDidAppear(animated)
        //更新约束
        avatarImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
            
        }
        //开始动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            // 对当前页面进行刷新
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 当上个动画完成后
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        // 发送通知 切换根控制器为MainVc
                    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: "welcome")
                })
        }
    }
}
